package com.abkj.platform.tail.controller;

import com.abkj.platform.tail.domain.Osmometer;
import com.abkj.platform.tail.domain.Rain;
import com.abkj.platform.tail.domain.Ultrasonic;
import com.abkj.platform.tail.service.BaseService;
import com.abkj.platform.tail.util.DateUtil;
import com.abkj.platform.tail.util.JsonUtils;
import com.abkj.platform.tail.vo.DataVO;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({ "analysis" })
public class AnalysisController extends BaseController {
	@RequestMapping({ "/ultrasonic" })
	public String ultrasonic(ModelMap model) {
		Ultrasonic ultrasonic = (Ultrasonic) this.baseService.loadObjectById(Ultrasonic.class, 1);
		model.put("ultrasonic", ultrasonic);
		return "/analysis/ultrasonic";
	}

	@RequestMapping({ "/level" })
	public String level(ModelMap model) {
		List<Map> basicDatas = this.baseService.findBySql("select * from level where type!=1 order by num");
		List<Map> baseDatas = this.baseService.findBySql("select * from level where type=1");
		float baseShould1 = 0.0F;
		float baseShould2 = 0.0F;
		float baseInit1 = 0.0F;
		float baseInit2 = 0.0F;
		for (Map m : baseDatas) {
			if (m.get("num").toString().equals("1")) {
				baseShould1 = NumberUtils.toFloat(m.get("should").toString());
				baseInit1 = NumberUtils.toFloat(m.get("init").toString());
			} else {
				baseShould2 = NumberUtils.toFloat(m.get("should").toString());
				baseInit2 = NumberUtils.toFloat(m.get("init").toString());
			}
		}
		model.put("baseShould1", Float.valueOf(baseShould1));
		model.put("baseInit1", Float.valueOf(baseInit1));
		model.put("baseShould2", Float.valueOf(baseShould2));
		model.put("baseInit2", Float.valueOf(baseInit2));
		model.put("data", JsonUtils.writeObjToString(basicDatas));
		model.put("datas", basicDatas);
		return "/analysis/level";
	}
	@RequestMapping({ "/displacement" })
	public String displacement(ModelMap model) {
		List<Map> basicDatas = this.baseService.findBySql("select * from displacement where type!=1 order by num");
		List<Map> baseDatas = this.baseService.findBySql("select * from displacement where type=1");
		float baseShould1 = 0.0F;
		float baseShould2 = 0.0F;
		float baseInit1 = 0.0F;
		float baseInit2 = 0.0F;
		for (Map m : baseDatas) {
			if (m.get("num").toString().equals("1")) {
				baseShould1 = NumberUtils.toFloat(m.get("should").toString());
				baseInit1 = NumberUtils.toFloat(m.get("init").toString());
			} else {
				baseShould2 = NumberUtils.toFloat(m.get("should").toString());
				baseInit2 = NumberUtils.toFloat(m.get("init").toString());
			}
		}
		model.put("baseShould1", Float.valueOf(baseShould1));
		model.put("baseInit1", Float.valueOf(baseInit1));
		model.put("baseShould2", Float.valueOf(baseShould2));
		model.put("baseInit2", Float.valueOf(baseInit2));
		model.put("data", JsonUtils.writeObjToString(basicDatas));
		model.put("datas", basicDatas);
		return "/analysis/displacement";
	}

	@RequestMapping({ "/ultrasonic_line" })
	public String ultrasonic_line() {
		return "/analysis/ultrasonic_line";
	}

	@RequestMapping({ "/osmometer" })
	public String osmometer(ModelMap model) {
		List<Map> sensors = this.baseService.findBySql("SELECT * FROM osmometer ORDER BY case when sectdes='断面一' then 1 when sectdes='断面二' then 2 when sectdes='断面一' then 1 when sectdes='断面二' then 2 when sectdes='断面一' then 1 when sectdes='断面二' then 2 when sectdes='断面一' then 1 when sectdes='断面二' then 2 when sectdes='断面一' then 1 when sectdes='断面二' then 2 when sectdes='断面三' then 3 when sectdes='断面四' then 4 when sectdes='断面五' then 5 when sectdes='断面六' then 6 when sectdes='断面七' then 7 when sectdes='断面八' then 8 when sectdes='断面九' then 9 when sectdes='断面十' then 10 else 99 end asc");
		Map map = new LinkedHashMap();

		for (Map m : sensors) {
			float offer = NumberUtils.toFloat(String.valueOf(m.get("offer")));
			m.put("cm", Double.valueOf(offer / 9.800000000000001D * 100.0D));
			String sectdes = String.valueOf(m.get("sectdes"));
			Integer count = (Integer) map.get(sectdes);
			if (count == null)
				map.put(sectdes, Integer.valueOf(1));
			else {
				map.put(sectdes, Integer.valueOf(count.intValue() + 1));
			}
		}

		model.put("sensors", sensors);
		model.put("maps", map);

		return "/analysis/osmometer";
	}

	@RequestMapping({ "/line" })
	public String line(ModelMap model) {
		List osmometers = this.baseService.findAllObjects(Osmometer.class);
		System.out.println(osmometers);
		model.put("osmometers", osmometers);
		return "/analysis/line";
	}

	@RequestMapping(value = { "/senajax" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String senajax(int sentype) {
		if (sentype == 1) {
			List osmometers = this.baseService.findAllObjects(Osmometer.class);
			return JsonUtils.writeObjToString(osmometers);
		}
		if (sentype == 2) {
			List levels = this.baseService.findBySql("SELECT * FROM LEVEL WHERE TYPE=0");
			return JsonUtils.writeObjToString(levels);
		}
		if (sentype == 3) {
			List ultrasonics = this.baseService.findAllObjects(Ultrasonic.class);
			return JsonUtils.writeObjToString(ultrasonics);
		}
		if (sentype == 4) {
			List rains = this.baseService.findAllObjects(Rain.class);
			return JsonUtils.writeObjToString(rains);
		}
		if (sentype == 5) {
			List displacements = this.baseService.findBySql("SELECT * FROM DISPLACEMENT WHERE TYPE=0");
			return JsonUtils.writeObjToString(displacements);
		}
		return "";
	}

	@RequestMapping(value = { "/sendataajax" }, produces = { "text/html;charset=UTF-8" })
	@ResponseBody
	public String sendataajax(int sentype, int senid, String begin, String end) {
		System.out.println("sentype=" + sentype + "&senid=" + senid + "&begin=" + begin + "&end=" + end);
		String table = "";
		if (sentype == 1)
			table = "osdata";
		else if (sentype == 2)
			table = "leveldata";
		else if (sentype == 3)
			table = "ultdata";
		else if (sentype == 4) {
			table = "raindata";
		}else if (sentype == 5)
			table = "displacementdata";
		String sql = "select * from " + table + " where sid=" + senid + " and createtime>='" + begin
				+ "' and createtime<='" + end + "'";
		List datas = this.baseService.findBySql(sql);

		Map ret = createData(sentype, datas);
		String str = JsonUtils.writeObjToString(ret);
		System.out.println("return json == " + str);
		return str;
	}

	private Map<String, Object> createData(int sentype, List<Map> datas) {
		Map ret = new HashMap();
		List ds = new ArrayList();

		if (sentype == 2) {
			String basicQuery = "SELECT * FROM level WHERE TYPE=1 limit 1";
			List map = this.baseService.findBySql(basicQuery);
			if ((map != null) && (map.size() != 0)) {
				Map basic = (Map) map.get(0);
				float standardval = NumberUtils.toFloat(String.valueOf(basic.get("val")));

				for (Map m : datas) {
					long x = DateUtil.strToLong(String.valueOf(m.get("createtime")), "yyyy-MM-dd HH:mm:ss");
					float y = NumberUtils.toFloat(String.valueOf(m.get("val")));
					DataVO dv = new DataVO();
					dv.setX(x);
					dv.setY(standardval - y);
					ds.add(dv);
				}
			}
		}else if (sentype == 5) {
			String basicQuery = "SELECT * FROM displacement WHERE TYPE=1 limit 1";
			List map = this.baseService.findBySql(basicQuery);
			if ((map != null) && (map.size() != 0)) {
				Map basic = (Map) map.get(0);
				float standardval = NumberUtils.toFloat(String.valueOf(basic.get("val")));

				for (Map m : datas) {
					long x = DateUtil.strToLong(String.valueOf(m.get("createtime")), "yyyy-MM-dd HH:mm:ss");
					float y = NumberUtils.toFloat(String.valueOf(m.get("val")));
					DataVO dv = new DataVO();
					dv.setX(x);
					dv.setY(standardval - y);
					ds.add(dv);
				}
			}
		}  else {
			List map;
			if (sentype == 3) {
				String basicQuery = "SELECT * FROM ultrasonic limit 1";
				map = this.baseService.findBySql(basicQuery);
				if ((map != null) && (map.size() != 0)) {
					Map basic = (Map) map.get(0);
					float lenh = NumberUtils.toFloat(String.valueOf(basic.get("lenh")));

					for (Map m : datas) {
						long x = DateUtil.strToLong(String.valueOf(m.get("createtime")), "yyyy-MM-dd HH:mm:ss");
						float y = NumberUtils.toFloat(String.valueOf(m.get("val")));
						DataVO dv = new DataVO();
						dv.setX(x);
						dv.setY(lenh - y);
						ds.add(dv);
					}
				}
			} else {
				for (Map m : datas) {
					long x = DateUtil.strToLong(String.valueOf(m.get("createtime")), "yyyy-MM-dd HH:mm:ss");
					float y = NumberUtils.toFloat(String.valueOf(m.get("val")));
					DataVO dv = new DataVO();
					dv.setX(x);
					dv.setY(y);
					ds.add(dv);
				}
			}
		}
		ret.put("data", ds);
		return ret;
	}
}